package com.chuangjiangx.member.coupon.web.controller;


import com.chuangjiangx.commons.BeanUtils;
import com.chuangjiangx.commons.CurrentThreadContext;
import com.chuangjiangx.commons.exception.BaseException;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.commons.response.CamelResponse;
import com.chuangjiangx.commons.response.Response;
import com.chuangjiangx.commons.response.ResponseUtils;
import com.chuangjiangx.member.coupon.ddd.application.MbrCouponApplication;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrHasCoupon;
import com.chuangjiangx.member.coupon.ddd.domain.service.CouponDomainService;
import com.chuangjiangx.member.basic.web.controller.BaseController;
import com.chuangjiangx.member.basic.web.interceptor.Login;
import com.chuangjiangx.member.coupon.web.response.ClaimCouponResponse;
import com.chuangjiangx.member.coupon.web.response.CouponDetailInfoResponse;
import com.chuangjiangx.member.coupon.web.response.CouponListResponse;
import com.chuangjiangx.member.coupon.web.response.MbrHasCouponDetailResponse;
import com.chuangjiangx.member.coupon.ddd.query.MbrCouponQuery;
import com.chuangjiangx.member.coupon.ddd.query.condition.MbrCouponListCondition;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrCouponDetail;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrCouponList;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrHasCouponDetail;
import com.chuangjiangx.member.coupon.ddd.dal.dto.MbrHasCouponList;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrHasCouponId;
import com.chuangjiangx.member.coupon.ddd.domain.model.WxAuditStatus;
import com.chuangjiangx.member.coupon.ddd.domain.model.WxSyncStatus;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrUserContext;
import com.chuangjiangx.member.basic.ddd.domain.model.MemberId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 会员卡券
 */
@RestController
@RequestMapping(value = "/h5/mbr-coupon",produces = "application/json;charset=UTF-8")
public class MbrCouponController extends BaseController {

    private final MbrCouponQuery mbrCouponQuery;
    private final MbrCouponApplication mbrCouponApplication;
    private final CouponDomainService couponDomainService;

    @Autowired
    public MbrCouponController(
            MbrCouponQuery mbrCouponQuery,
            MbrCouponApplication mbrCouponApplication,
            CouponDomainService couponDomainService) {
        this.mbrCouponQuery = mbrCouponQuery;
        this.mbrCouponApplication = mbrCouponApplication;
        this.couponDomainService = couponDomainService;
    }

    /**
     * 卡券广场列表
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @map data
     * @see CouponListResponse
     */
    @RequestMapping(value = "/coupon-list")
    @Login
    public Response couponList() {
        MbrUserContext userContext = (MbrUserContext) CurrentThreadContext.getCurrentUser();
        MbrCouponListCondition condition = new MbrCouponListCondition();
        condition.setMerchantId(userContext.getMerchantId());
        //H5过滤活动过期的卡券
        condition.setActTimeEnd(new Date());
        condition.setStatuses(Arrays.asList(1,2));
        condition.setPageSize(Integer.MAX_VALUE);
        PagingResult<MbrCouponList> listForMerchant = mbrCouponQuery.findMbrCouponListForMerchant(condition);
        List<CouponListResponse> listResponses = BeanUtils.convertCollection(listForMerchant.getItems(), CouponListResponse.class);
        return ResponseUtils.successCamel(listResponses);
    }

    /**
     * 领取卡券
     * @param couponNumber 卡券编号
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @see ClaimCouponResponse
     */
    @RequestMapping(value = "/claim-coupon")
    @Login
    public Response claimCoupon(String couponNumber) {
        MbrUserContext userContext = (MbrUserContext) CurrentThreadContext.getCurrentUser();
        if(!isValidLogin(userContext,userContext.getMerchantId())){
            throw new BaseException("000006","失效的登录状态");
        }
        MbrHasCoupon mbrHasCoupon = mbrCouponApplication.memberClaimCoupon(new MemberId(userContext.getMemberId()), couponNumber);
        ClaimCouponResponse response = new ClaimCouponResponse();
        response.setMbrHasCouponId(mbrHasCoupon.getId().getId());
        return ResponseUtils.successCamel(response);
    }

    /**
     * 卡券详情
     * @param couponNumber 卡券编号
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @see CouponDetailInfoResponse
     */
    @RequestMapping(value = "/coupon-detail")
//    @Login
    public Response couponDetailInfo(String couponNumber) {
        MbrCouponDetail detailInfo = mbrCouponQuery.getMbrCouponDetailInfo(couponNumber);
        CouponDetailInfoResponse response = new CouponDetailInfoResponse();
        BeanUtils.convertBean(detailInfo,response);
        response.setStoreLists(detailInfo.getStoreLists());
        return ResponseUtils.successCamel(response);
    }

    /**
     * 会员拥有卡券列表
     * @param verifyStatus 核销状态 0-未使用 1-已使用 2-已过期 3-已冻结
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @map data
     * @see CouponListResponse
     */
    @RequestMapping(value = "/mbr-has-coupon-list")
    @Login
    public Response mbrHasCouponList(Integer verifyStatus, HttpServletRequest request) {
        MbrUserContext userContext = (MbrUserContext) CurrentThreadContext.getCurrentUser();
        if (verifyStatus == null) {
            verifyStatus = Integer.valueOf(request.getParameter("verifyStatus"));
        }
        List<MbrHasCouponList> mbrHasCouponList = mbrCouponQuery.findMbrHasCouponList(userContext.getMemberId(), verifyStatus);
        List<CouponListResponse> listResponses = BeanUtils.convertCollection(mbrHasCouponList, CouponListResponse.class);
        return ResponseUtils.successCamel(listResponses);
    }

    /**
     * 会员已领取卡券详情
     * @param mbrHasCouponId 会员拥有卡券id
     * @return com.chuangjiangx.commons.response.CamelResponse
     * @see MbrHasCouponDetailResponse
     */
    @RequestMapping(value = "/mbr-has-coupon-detail")
    @Login
    public CamelResponse<MbrHasCouponDetailResponse> mbrHasCouponDetailInfo(Long mbrHasCouponId, HttpServletRequest request) {
        if (mbrHasCouponId == null) {
            mbrHasCouponId = Long.valueOf(request.getParameter("mbrHasCouponId"));
        }
        couponDomainService.checkAuditStatus(mbrHasCouponId);
        MbrHasCouponDetail detail = mbrCouponQuery.getMbrHasCouponDetail(new MbrHasCouponId(mbrHasCouponId));
        MbrHasCouponDetailResponse response = new MbrHasCouponDetailResponse();
        BeanUtils.convertBean(detail,response);
        response.setStoreLists(detail.getStoreLists());
        if(Objects.equals(WxAuditStatus.SUCCESS.code,detail.getWxAuditStatus())&&Objects.equals(WxSyncStatus.NOT_SYNC.code,detail.getWxSyncStatus())){
            response.setCanSync(1);
        }else{
            response.setCanSync(0);
        }
        return (CamelResponse<MbrHasCouponDetailResponse>)ResponseUtils.successCamel(response);
    }

}
